package com.shuda.business.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuda.business.crm.entity.CustomerTransferEntity;
import com.shuda.business.crm.mapper.CustomerTransferMapper;
import com.shuda.business.crm.service.CustomerTransferService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 客户转移记录Service实现类
 * @author shuda
 */
@Service
@RequiredArgsConstructor
public class CustomerTransferServiceImpl extends ServiceImpl<CustomerTransferMapper, CustomerTransferEntity> 
        implements CustomerTransferService {

    private final CustomerTransferMapper customerTransferMapper;

    @Override
    public IPage<CustomerTransferEntity> getCustomerTransferPage(Page<CustomerTransferEntity> page, 
                                                               Long customerId, 
                                                               String transferStatus, 
                                                               String applicantName) {
        LambdaQueryWrapper<CustomerTransferEntity> queryWrapper = new LambdaQueryWrapper<>();
        
        if (customerId != null) {
            queryWrapper.eq(CustomerTransferEntity::getCustomerId, customerId);
        }
        
        if (transferStatus != null && !transferStatus.trim().isEmpty()) {
            queryWrapper.eq(CustomerTransferEntity::getTransferStatus, transferStatus);
        }
        
        if (applicantName != null && !applicantName.trim().isEmpty()) {
            queryWrapper.like(CustomerTransferEntity::getApplicantName, applicantName);
        }
        
        queryWrapper.orderByDesc(CustomerTransferEntity::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<CustomerTransferEntity> getCustomerTransferByCustomerId(Page<CustomerTransferEntity> page, Long customerId) {
        return customerTransferMapper.selectPageByCustomerId(page, customerId);
    }

    @Override
    public CustomerTransferEntity getCustomerTransferById(Long id) {
        return this.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createCustomerTransfer(CustomerTransferEntity transferEntity) {
        transferEntity.setTransferStatus("pending");
        transferEntity.setApplyTime(LocalDateTime.now());
        return this.save(transferEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCustomerTransfer(CustomerTransferEntity transferEntity) {
        return this.updateById(transferEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCustomerTransfer(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteCustomerTransfer(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean approveCustomerTransfer(Long transferId, String approveStatus, String approveRemark, Long approverId, String approverName) {
        CustomerTransferEntity transferEntity = this.getById(transferId);
        if (transferEntity == null) {
            return false;
        }
        
        transferEntity.setTransferStatus(approveStatus);
        transferEntity.setApproveRemark(approveRemark);
        transferEntity.setApproverId(approverId);
        transferEntity.setApproverName(approverName);
        transferEntity.setApproveTime(LocalDateTime.now());
        
        return this.updateById(transferEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchApproveCustomerTransfer(Long[] transferIds, String approveStatus, String approveRemark, Long approverId, String approverName) {
        LocalDateTime approveTime = LocalDateTime.now();
        
        for (Long transferId : transferIds) {
            CustomerTransferEntity transferEntity = this.getById(transferId);
            if (transferEntity != null) {
                transferEntity.setTransferStatus(approveStatus);
                transferEntity.setApproveRemark(approveRemark);
                transferEntity.setApproverId(approverId);
                transferEntity.setApproverName(approverName);
                transferEntity.setApproveTime(approveTime);
                this.updateById(transferEntity);
            }
        }
        
        return true;
    }
}